NGINX 部署

NGINX 是一个高性能的 HTTP 和反向代理服务器,以下是部署 NGINX 的基本步骤:

1. 安装 NGINX

在 Ubuntu/Debian 上安装

sudo apt update
sudo apt install nginx

在 CentOS/RHEL 上安装

sudo yum install epel-release
sudo yum install nginx

2. 启动 NGINX 服务

sudo systemctl start nginx
sudo systemctl enable nginx  # 设置开机自启

3. 验证安装

访问服务器 IP 地址,应该能看到 NGINX 欢迎页面。

4. 基本配置

NGINX 的主要配置文件位于:

  • /etc/nginx/nginx.conf - 主配置文件
  • /etc/nginx/sites-available/ - 虚拟主机配置
  • /etc/nginx/sites-enabled/ - 启用的虚拟主机(通常是 sites-available 的符号链接)

示例配置(静态网站)

nginx

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

5. 测试并重载配置

sudo nginx -t  # 测试配置语法
sudo systemctl reload nginx  # 重载配置

6. 防火墙设置

如果使用防火墙,需要允许 HTTP/HTTPS 流量:

sudo ufw allow 'Nginx Full'  # Ubuntu
# 或
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload  # CentOS

7. SSL 证书配置(可选)

使用 Let's Encrypt 免费 SSL 证书:

sudo apt install certbot python3-certbot-nginx  # Ubuntu
sudo certbot --nginx -d example.com -d www.example.com

8. 常用命令

  • 启动:sudo systemctl start nginx
  • 停止:sudo systemctl stop nginx
  • 重启:sudo systemctl restart nginx
  • 重载配置:sudo systemctl reload nginx
  • 检查状态:sudo systemctl status nginx
  • 测试配置:sudo nginx -t

高级功能

  1. 反向代理

    nginx

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    
  2. 负载均衡

    nginx

    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
    }
    
    server {
        location / {
            proxy_pass http://backend;
        }
    }
    
  3. 缓存配置

    nginx

    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m inactive=60m;
    
    server {
        location / {
            proxy_cache my_cache;
            proxy_pass http://backend;
        }
    }
    

Nginx反向代理jumpserver配置参考

Nginx服务器ip:10.200.11.17

jumpserver服务器ip:10.200.11.15

域名:以实际域名填写,我这边以jumpserver.xxxx.com为例

# /etc/nginx/conf.d/jumpserver.conf

# 主HTTP服务(监听8080,用于HTTP跳转HTTPS)
server {
    listen 8080;
    server_name jumpserver.xxxx.com;
    return 301 https://$host$request_uri;  # 跳转到HTTPS
}

# HTTPS核心服务
server {
    listen 443 ssl http2;
    server_name jumpserver.xxxx.com;

    # SSL证书配置
    ssl_certificate /etc/nginx/ssl/jumpserver.xxxx.com.pem;
    ssl_certificate_key /etc/nginx/ssl/jumpserver.xxxx.com.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    # HSTS 头部 (可选但推荐)
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    # 安全头部增强
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self' wss:;";
    client_max_body_size 5000m;  # 上传文件大小限制
    # 日志配置
    access_log /var/log/nginx/jumpserver.access.log;
    error_log /var/log/nginx/jumpserver.error.log;
    # 代理配置
    location / {
        proxy_pass http://10.200.11.15:80;  # 直接指向Docker的80端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_redirect off;
        # 增加超时设置
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        send_timeout 300;
    }
    # WebSocket 支持(用于 Guacamole 等组件)
    location /ws/ {
        proxy_pass http://10.200.11.15:80;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        # WebSocket 超时设置
        proxy_connect_timeout 300;
        proxy_send_timeout 3600;  # 长连接超时设置
        proxy_read_timeout 3600;  # 长连接超时设置
        proxy_buffering off;
        # 禁用 Nagle 算法,提高实时性
        proxy_set_header TCP_NODELAY on;
    }
}

注意

v3.6 版本为了安全,要求强制填写 DOMAINS 可信任域名才能正常访问服务,否则会提示错误码 400/403 导致无法无法访问页面,DOMAINS 配置如下。

如果服务器是一键安装并且旧版本就已经使用 JumpServer 开启了 HTTPS,则不需要进行任何更改。 需要使用 IP 地址来访问 JumpServer 的场景,可以根据自己的 IP 类型来填写 config.txt 配置文件中 DOMAINS 字段为公网 IP 还是内网 IP。

打开config.txt 配置文件,定义 DOMAINS 字段 vim /opt/jumpserver/config/config.txt

可信任 DOMAINS 定义,

定义可信任的访问 IP, 请根据实际情况修改, 如果是公网 IP 请改成对应的公网 IP。

例如:DOMAINS="demo.jumpserver.org" # 使用域名访问

例如:DOMAINS="172.17.200.191" # 使用 IP 访问

例如:DOMAINS="demo.jumpserver.org,172.17.200.191" # 使用 IP 和 域名一起访问

DOMAINS="jumpserver.xxxx.com,10.200.11.15" #实际环境中我的配置

重启 JumpServer 服务生效

jmsctl restart

Copyright © https://yan-jian.com 2023 all right reserved更新时间: 2025-07-16 18:29:39

results matching ""

    No results matching ""